package com.agiantii.ablog.controller;

import com.agiantii.ablog.common.R;
import com.agiantii.ablog.pojo.dto.UserLoginDTO;
import com.agiantii.ablog.pojo.dto.UserRegisterDTO;
import com.agiantii.ablog.pojo.User;
import com.agiantii.ablog.service.UserService;
import com.agiantii.ablog.pojo.vo.UserVO;
import com.agiantii.ablog.pojo.vo.UserStatsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("users")
public class UserController {
    
    @Resource
    private UserService userService;
    
    @PostMapping("register")
    public R<User> register(UserRegisterDTO registerDTO) {
        log.info("register registerDTO:{}", registerDTO);
        return userService.register(registerDTO);
    }
    
    @PostMapping("login")
    public R<User> login(UserLoginDTO loginDTO) {
        log.info("login loginDTO:{}", loginDTO);
        return userService.login(loginDTO);
    }
    
    @PutMapping("")
    public R<User> updateProfile(User user, Long userId) {
        log.info("updateProfile userId:{}, user:{}", userId, user);
        return userService.updateProfile(userId, user);
    }
    
    @PostMapping("follow")
    public R<Void> follow(Long targetUserId, Long userId) {
        log.info("follow currentUserId:{}, userId:{}", targetUserId, userId);
        return userService.follow(userId,targetUserId);
    }
    
    @PostMapping("unfollow")
    public R<Void> unfollow(Long targetUserId, Long userId) {
        log.info("unfollow currentUserId:{}, userId:{}", targetUserId, userId);
        return userService.unfollow(userId,targetUserId);
    }
    
    @GetMapping("/{userId}")
    public R<User> getUserInfo(@PathVariable Long userId) {
        return userService.getUserInfo(userId);
    }
    
    @GetMapping("list")
    public R<List<UserVO>> getUserList(Integer page, Integer size) {
        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        return userService.getUserList(page, size);
    }
    
    @GetMapping("{userId}/followers")
    public R<List<UserVO>> getFollowers(@PathVariable Long userId) {
        return userService.getFollowers(userId);
    }
    
    @GetMapping("{userId}/following")
    public R<List<UserVO>> getFollowing(@PathVariable Long userId) {
        return userService.getFollowing(userId);
    }
    
    @GetMapping("{userId}/stats")
    public R<UserStatsVO> getUserStats(@PathVariable Long userId) {
        log.info("getUserStats userId:{}", userId);
        return userService.getUserStats(userId);
    }
} 